<!DOCTYPE html>
<html>
<head>
	<title>分配资源</title>
	<link rel="stylesheet" href="${ctx}/static/admin/layui/css/layui.css">
</head>
<body>
<div class="layui-card layui-col-xs12">
	<div class="layui-col-xs6">
		<div class="layui-card-header">
			<p>按钮权限</p>
		</div>
		<dl class="layui-anim layui-anim-upbit">
			<dd>
				<ul id="classtree"></ul>
			</dd>
		</dl>
	</div>
	<div class="layui-col-xs6">
		<div class="layui-card-header">
			<p>您正在给“${module.name}”模块分配人员权限</p>
		</div>
		<div class="layui-card-body">
			<form class="layui-form">
				<ul id="restree" class="ztree"></ul>
				<div class="layui-form-item" style="display: none">
					<div class="layui-input-block">
						<button class="layui-btn" id="submit-btn" lay-submit>立即提交</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
</body>
</html>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<link rel="stylesheet" href="${ctx}/static/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${ctx}/static/jquery/jquery.min.js" charset="utf-8"></script>
<script type="text/javascript" src="${ctx}/static/ztree/jquery.ztree.all.min.js" charset="utf-8"></script>
<script>
    var table;
    layui.use(['jquery','form','tree'], function(){
        var $ = jQuery = layui.$;
        var form = layui.form;
        var tree = layui.tree;

        $.fn.zTree.init($("#restree"), setting, zNodes);
        zTree_Menu = $.fn.zTree.getZTreeObj("restree");

        $.ajax({
            url:"${ctx}/sys_admin/sys/res/getChildren?pid=${module.sysres!}",
            dataType:'json',
            async:false,
            success:function(result){
                tree.render({
                    id: 'funTree',
                    elem: '#classtree',
                    showCheckbox: true,
                    nodes: result,
                    data: result
                });
            }
        });

        //监听提交
        form.on('submit', function(data){
            var nodes = zTree_Menu.getCheckedNodes(true);
            var selectIds = "";
            var resId = "${module.sysres!}";
            var checkData = tree.getChecked('funTree');
            if (checkData.length == 0) {
                layer.msg('请选择按钮权限！');
                return false;
			}
            $.each(checkData, function(i, item) {
                resId+=","+item.id;
			});
            for(var index in nodes){
                var item=nodes[index];
                if (item.id > 0) {
                    if(item.id=='10000')continue;
                    selectIds+=item.id+",";
                }
            }
            var submitData={
                "userIds":selectIds,
                "resId":resId,
            };
            $.post("${ctx}/sys_admin/modules/saveModuleUser" , submitData ,
                function(result){
                    if (result.success) {
                        table.reload({});
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index); //再执行关闭
                    } else {
                        layer.msg(result.msg);
                    }
                },"json");
            return false;
        });


        $.ajax({
            url:"${ctx}/sys_admin/sys/res/getChildren?pid=${module.sysres!}",
            dataType:'json',
            success:function(result){
                tree.render({
                    id: 'funTree',
                    elem: '#classtree',
                    showCheckbox: true,
                    nodes: result,
                    data: result
                });
            }
        });
    });

    var zTree_Menu;
    var zNodes = ${restree.data};
    var setting = {
        check: {
            enable: true
        },
        callback : {
            beforeCheck: beforeCheck,
            onCheck: onCheck

        },
        data : {
            simpleData : {
                enable : true
            }
        }
    }

    function beforeCheck(treeId, treeNode) {
        return (treeNode.doCheck !== false);
    }

    function onCheck(e, treeId, treeNode) {

    }
</script>